package cn.mw.cmdb.entity;

import cn.mw.components.mongodb.anno.AutoId;
import cn.mw.components.mongodb.anno.CMDBProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.List;

@Data
@Document("mw_cmdbmd_model_catolog")
public class ModelPropertyCatolog implements Comparable{
    public static final String DefaultName = "默认属性";
    @Schema(description = "分类id")
    @AutoId
    private String id;

    @Schema(description = "模型id")
    @CMDBProperty
    private String modelId;

    @Schema(description = "分类名称")
    @CMDBProperty
    private String name;

    @Schema(description = "排序")
    @CMDBProperty
    private int order;

    public static ModelPropertyCatolog build(String modelId, String name){
        ModelPropertyCatolog ret = new ModelPropertyCatolog();
        ret.setModelId(modelId);
        ret.setName(name);
        if(DefaultName.equals(name)){
            ret.setOrder(1);
        }else{
            ret.setOrder(2);
        }

        return ret;
    }

    public static boolean checkDefault(List<ModelPropertyCatolog> list){
        boolean ret = false;
        for(ModelPropertyCatolog modelPropertyCatolog : list){
            if(DefaultName.equals(modelPropertyCatolog.getName())){
                ret = true;
                break;
            }
        }
        return ret;
    }

    @Override
    public int compareTo(Object o) {
        ModelPropertyCatolog dest = (ModelPropertyCatolog)o;
        if(this.order < dest.getOrder()){
            return -1;
        }else if(this.order > dest.getOrder()){
            return 1;
        }
        return this.name.compareTo(dest.getName());
    }
}
